/*
 Copyright (c) 2013, Ike Yousuf <admin@kudodev.com>
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 names of its contributors may be used to endorse or promote products
 derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.kudo.gdx;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.kudo.gdx.ui.UIDisplay;
import org.lwjgl.input.Keyboard;

/**
 *
 * @author Ike
 */
public abstract class GameContainer extends InputAdapter implements ApplicationListener {

    private int width, height;
    private Graphics g = null;
    private InputMultiplexer inputMultiplexer;
    private UIDisplay gui = null;
    private boolean showFPS = false;

    public GameContainer(int w, int h) {
        width = w;
        height = h;
    }

    @Override
    public void create() {
        try {
            Keyboard.enableRepeatEvents(true);
            OrthographicCamera camera = new OrthographicCamera();
            camera.setToOrtho(true, width, height);
            g = new Graphics(new SpriteBatch(), camera, new ShapeRenderer());

            //input setup
            inputMultiplexer = new InputMultiplexer(this);
            Gdx.input.setInputProcessor(inputMultiplexer);

            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void createGUI() {
        if (gui == null) {
            gui = new UIDisplay();
            inputMultiplexer.addProcessor(0, gui);
        }
    }

    public void setShowFPS(boolean b) {
        showFPS = b;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public UIDisplay getGUI() {
        return gui;
    }

    @Override
    public void render() {
        float d = Gdx.graphics.getDeltaTime();
        update(d);

        Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        g.setColor(Color.WHITE);


        g.textureBatchBegin();
        draw(g);
        if (gui != null) {
            gui.update(d);
            gui.render(g);
        }
        if (showFPS) {
            g.drawString("FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 50);
        }
        g.textureBatchEnd();
    }

    @Override
    public void resize(int x, int y) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        g.dispose();
    }

    public abstract void init();

    public abstract void update(float delta);

    public abstract void draw(Graphics g);

    //------Start Input------//
    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
    //------End Input------//
}
